﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace WindowsFormsApplication1.wtyczki
{
    static class Contrast
    {
        public static Bitmap ContrastThis(Bitmap bm)
        {
            int nValue;
            Bitmap bmTmp = new Bitmap(bm);
            using (ScrollBar form2 = new ScrollBar("Kontrast",100))
            {
                form2.ShowDialog();
                nValue = form2.TheValue;
            }
            for(int y=1;y<bm.Height;y++)
            {
                for(int x=1;x<bm.Width;x++)
                {
                    int r,g,b,R,G,B;
                    r=bm.GetPixel(x,y).R;
                    g=bm.GetPixel(x,y).G;
                    b=bm.GetPixel(x,y).B;
                    R=(255/(255-2*nValue))*(r-nValue);
                    G=(255/(255-2*nValue))*(g-nValue);
                    B=(255/(255-2*nValue))*(b-nValue);
                    if(R<nValue)
                    {
                        R=0;
                    }
                    if(G<nValue)
                    {
                        G=0;
                    }
                    if(B<nValue)
                    {
                        B=0;
                    }

                    if(R>255-nValue)
                    {
                        R=255;
                    }
                    if(G>255-nValue)
                    {
                        G=255;
                    }
                    if(B>255-nValue)
                    {
                        B=255;
                    }
                    bmTmp.SetPixel(x,y,Color.FromArgb(R,B,G));
                }
            }
             

            return bmTmp;
        }
    }
}
